#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h> 
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
 
//#define CMD1 1  
//#define CMD2 2 
//#define CMD3 3  
//#define     TYPE            'S' 
//#define     LEVEL_DOWN      _IOWR(TYPE, 0, int)
//#define     LEVEL_UP        _IOWR(TYPE, 1, int)
//#define     GPIO_INPUT      _IOWR(TYPE, 2, int)
//#define     GPIO_OUTPUT     _IOWR(TYPE, 3, int)
//#define     GPIO_GET        _IOWR(TYPE, 4, int)
int main(int argc, char **argv)
{
    int fd;
    int file;
    int val = 1;
    char data[256] = {0};
    fd = open("/dev/hello", O_RDWR);
    //file = open("hellofile", O_RDWR);
    
    if (fd == -1) {
        printf("0-can't open...\n");
        exit(-1);
    } else {
        printf("1-open ok...use 'dmesg | tail -12' to see 12 lines printk\n");
	int retval;
        retval = write(fd, "yourname", 8);//8 length
        if (retval == -1) {
            perror("write error\n");
            close(fd);
            exit(-1);
        } else {
            printf("2-write ok...\n");
            retval = read(fd, data, 10);
            if (retval == -1) {
                perror("read error\n");
                close(fd);
                exit(-1);
            }
            printf("3-read ok: %s\n", data);

	    unsigned int CMD;
    	    printf("请输入CMD：");
    	    scanf("%d", &CMD);  

	    if(CMD==2){
		retval = ioctl(fd, 4, 0);
	    }else{
		retval = ioctl(fd, CMD, 0);
	    }
            
            if (retval == -1) {
                perror("ioctl error\n");
                close(fd);
                exit(-1);
            }
            printf("4-ioctl CMD%d ok\n",CMD);
            close(fd);
        }

    }

    exit(EXIT_SUCCESS);
    return 0;
}
